var BaseLayer = {
	newLayer:function(options){
		this.layerMarkers = [];
		this.layerZoom = null;
		this.layerCenter = null;
		this.isVisible = false;
		var layer = sbl.mergeObjects(options,this,false);
		return layer;
	},
	getLayerName:function(){
		return this.layerName
	},
	getVisible:function(){
		var boss = this;
		return boss.isVisible;
	},
	setVisible:function(visible){
		var boss = this;
		boss.isVisible = visible;
		if(visible){
			boss.showLayer();
		}
		else{
			boss.hideLayer();
		}
	},
	drawLayer:function(){
		var boss = this;
		var map = boss.map;
		var bound = map.getBounds();
		var miny = bound._swLat;
		var minx = bound._swLng;
		var maxy = bound._neLat;
		var maxx = bound._neLng;
		
		if(map.getZoom() < 15){
			boss.hideLayer();
			return;
		}

		if(!boss.isOutOfView()){
			return;
		}
		
		boss.layerZoom = map.getZoom();
		boss.layerCenter = map.getCenter();
		
		boss.clearMarkers();
		boss.isVisible = true;
		
		sbl.ajax({
			dataType:'json',
			type:'get',
			data:{miny:miny,minx:minx,maxy:maxy,maxx:maxx},
			url:boss.dataUrl,
			success:function(d){
				var data = d.data;
				for(var j=0;j<data.length;j++){
					var row = data[j];
					var point = new BMap.Point(row.LON,row.LAT);
					var Marker = new BMap.Marker(point);
					if(boss.icon != null){
						var icon = new BMap.Icon(boss.icon.iconUrl, new BMap.Size(boss.icon.width,boss.icon.height));
						Marker.setIcon(icon);
					}
					
					Marker.data = row;		
					
					if(boss.onMarkerClick != null){
						Marker.addEventListener("click",function(){
							boss.onMarkerClick(this);
						});
					}
					
					boss.layerMarkers.push(Marker);
					map.addOverlay(Marker);
				}
			}
		});
	},
	removeLayer:function(){
		var boss = this;
		boss.clearMarkers();
	},
	showLayer:function(){
		var boss = this;
		var isOutOfView = boss.isOutOfView();
		if(isOutOfView){
			boss.drawLayer();
		}
		else{
			for(var i=0;i<boss.layerMarkers.length;i++){
				var Marker = boss.layerMarkers[i];
				Marker.show();
			}
		}
	},
	hideLayer:function(){
		var boss = this;
		for(var i=0;i<boss.layerMarkers.length;i++){
			var Marker = boss.layerMarkers[i];
			Marker.hide();
		}
	},
	clearMarkers:function(){
		var boss = this;
		var map = boss.map;
		for(var i=0;i<boss.layerMarkers.length;i++){
			var Marker = boss.layerMarkers[i];
			map.removeOverlay(Marker);
		}
		boss.layerMarkers = [];
	},
	isOutOfView:function(){
		var boss = this;
		
		if(boss.layerZoom == null || boss.layerCenter == null){
			return true;
		}
		
		var map = boss.map;
		var resolution = Math.pow(2,(19 - map.getZoom())) * 117 / 256;
		var zoomOff = Math.abs(map.getZoom() - boss.layerZoom);
		var centerOff = map.getDistance(boss.layerCenter,map.getCenter()) / resolution;
		
		if(zoomOff > 0){
			return true;
		}
		
		if(centerOff >= 300){
			return true;
		}
		
		return false;
	}
}